Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
The 'iferr' npm package is a utility for handling errors in Node.js callbacks. It simplifies the process of checking for errors and executing corresponding error-handling code, making the code more readable and maintainable.
Basic Error Handling
This feature allows you to handle errors and success cases in a more readable way. The 'iferr' function takes two arguments: an error handler and a success handler. If an error occurs, the error handler is executed; otherwise, the success handler is executed.
const iferr = require('iferr');
function someAsyncFunction(callback) {
// Simulate an asynchronous operation
setTimeout(() => {
const error = Math.random() > 0.5 ? new Error('Something went wrong!') : null;
const result = error ? null : 'Success!';
callback(error, result);
}, 1000);
}
someAsyncFunction(iferr((err) => {
console.error('Error:', err);
}, (result) => {
console.log('Result:', result);
}));
Custom Error Handling
This feature allows you to define custom error and success handlers that can be reused across different asynchronous functions. This promotes code reuse and consistency in error handling.
const iferr = require('iferr');
function someAsyncFunction(callback) {
// Simulate an asynchronous operation
setTimeout(() => {
const error = Math.random() > 0.5 ? new Error('Something went wrong!') : null;
const result = error ? null : 'Success!';
callback(error, result);
}, 1000);
}
const customErrorHandler = iferr((err) => {
console.error('Custom Error Handler:', err);
}, (result) => {
console.log('Custom Success Handler:', result);
});
someAsyncFunction(customErrorHandler);
The 'async' package provides a collection of functions for working with asynchronous JavaScript. It includes utilities for handling errors in asynchronous operations, such as 'async.waterfall' and 'async.series'. Compared to 'iferr', 'async' offers a broader range of functionalities for managing asynchronous control flow.
The 'promise' package is a lightweight implementation of Promises/A+ for Node.js and browsers. It allows for more modern and flexible error handling using promises and async/await syntax. While 'iferr' focuses on callback-based error handling, 'promise' is more suitable for applications that use promises.
The 'bluebird' package is a fully-featured promise library for JavaScript. It provides advanced features like cancellation, iteration, and error handling. 'bluebird' is more powerful and versatile compared to 'iferr', which is specifically designed for simplifying callback-based error handling.
Higher-order functions for easier error handling.
if (err) return cb(err);
be gone!
npm install iferr
var iferr = require('iferr');
function get_friends_count(id, cb) {
User.load_user(id, iferr(cb, user =>
user.load_friends(iferr(cb, friends =>
cb(null, friends.length)
))
))
}
var iferr = require('iferr');
function get_friends_count(id, cb) {
User.load_user(id, iferr(cb, function(user) {
user.load_friends(iferr(cb, function(friends) {
cb(null, friends.length)
}))
}))
}
iferr = require 'iferr'
get_friends_count = (id, cb) ->
User.load_user id, iferr cb, (user) ->
user.load_friends iferr cb, (friends) ->
cb null, friends.length
(TODO: document tiferr, throwerr and printerr)
MIT
FAQs
Higher-order functions for easier error handling
The npm package iferr receives a total of 1,579,381 weekly downloads. As such, iferr popularity was classified as popular.
We found that iferr demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.